<?php
/**
 * Created by PhpStorm.
 * User: moon
 * Date: 2019/1/8
 * Time: 09:06
 */

namespace application\controller;
use application\depend\Appresult;
use application\depend\Regular;

class ApiCore
{
    protected $postData = null;
    protected $returnData = null;
    protected $userInfo = null;  //当前用户信息
    protected $continue = true;  //是否继续的标记

    public function __construct($data)
    {
        $this->postData = $data;
        $this->returnData = new Appresult();
        unset($data);
        //初始化用户身份参数
        $this->userInfo = [
          'userId' => '0',
          'sign' => null,
          'timestamp' => null,
        ];
        //初始化用户身份信息
        $this->initUserId();
    }//end function

    protected function returnResult(){
        $this->postData = null;
        $this->userInfo = null;
        return $this->returnData;
    }

    //初始化参数
    protected function initParam(){

    }//end function

    //初始化用户身份信息
    protected function initUserId(){
        if(isset($this->postData['userId']) && !empty($this->postData['userId'])){
            if(!Regular::checkInt($this->postData['userId'])){
                $this->returnData->result = ERR_PARAM_ERROR;
                $this->returnData->msg = "非法用户ID[v47]";
                $this->continue = false;
                return false;
            }else{
                $this->userInfo['userId'] = $this->postData['userId'];
            }
        }//end if

        if(isset($this->postData['sign']) && !empty($this->postData['sign'])){
            if(!Regular::checkMd5Sign($this->postData['sign'])){
                $this->returnData->result = ERR_PARAM_ERROR;
                $this->returnData->msg = "非法签名[v57]";
                $this->continue = false;
                return false;
            }else{
                $this->userInfo['sign'] = $this->postData['sign'];
            }
        }//end if

        if(isset($this->postData['timestamp']) && !empty($this->postData['timestamp'])){
            if(!Regular::checkTimestamp($this->postData['timestamp'])){
                $this->returnData->result = ERR_PARAM_ERROR;
                $this->returnData->msg = "非法签名格式[v70]";
                $this->continue = false;
                return false;
            }else{
                $this->userInfo['timestamp'] = $this->postData['timestamp'];
            }
        }//end if

        if(isset($this->postData['appCode']) && !empty($this->postData['appCode'])){
            if(!Regular::checkEnNumStr($this->postData['appCode'])){
                $this->returnData->result = ERR_PARAM_ERROR;
                $this->returnData->msg = "非法app编码[v82]";
                $this->continue = false;
                return false;
            }
            $this->userInfo['appCode'] = $this->postData['appCode'];
        }


    }//end function

    protected function setReturnData($result = "", $msg = "",$rows = null){
        $this->returnData->result = $result;
        $this->returnData->msg = $msg;
        if(!empty($rows)){
            $this->returnData->rows = $rows;
        }
    }

    /**
     * @param string $postName
     * @param string $regName
     * @return bool
     */
    protected function checkParam($postName = "",$regName = ""):bool{
        if(isset($this->postData[$postName]) && !empty($this->postData[$postName])){
            if(Regular::$regName($this->postData[$postName])){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }//end

    protected function needUserInfo(){
        if($this->continue) {
            //为什么要这个判断 如果这个是true 就有可能没有经过initUserId 这个方法 一般不存在这个可能
            if (empty($this->userInfo['userId'])) {
                $this->setReturnData(ERR_PARAM_ERROR, '缺少用户信息！');
                $this->continue = false;
            }//end if
            else if (empty($this->userInfo['sign'])) {
                $this->setReturnData(ERR_PARAM_ERROR, '非法签名信息！');
                $this->continue = false;
            } else if (empty($this->userInfo['timestamp'])) {
                $this->setReturnData(ERR_PARAM_ERROR, '非法请求！');
                $this->continue = false;
            }
        }//end if
    }//end if
}//end class